package org.yufan.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.yufan.admin.service.BaseService;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * Created by zxd on 2019/3/5
 **/
public abstract class BaseServiceImpl<T> implements BaseService<T> {

    @Autowired  //泛型注入
    private Mapper<T> mapper;

    private Class clazz;


    public BaseServiceImpl(){
        ParameterizedType parameterizedType= (ParameterizedType) this.getClass().getGenericSuperclass();
        clazz= (Class) parameterizedType.getActualTypeArguments()[0];
        System.out.println(clazz+"--------------bbb");
    }

    @Override
    public void insert(T t) {
        mapper.insertSelective(t);
    }

    @Override
    public void update(T t) {
       mapper.updateByPrimaryKeySelective(t);
    }

    @Override
    public void deleteById(Long id) {
         mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByProperty(List<Object> list, String property) {
        Example example=new Example(clazz);
        example.createCriteria().andIn(property,list);
        mapper.deleteByExample(example);
    }

    @Override
    public T findById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public T findByWhere(T t) {
        return mapper.selectOne(t);
    }

    @Override
    public List<T> findByWhereList(T t) {
        return mapper.select(t);
    }

    @Override
    public PageInfo<T> findByPageWhere(T t, int page, int rows) {
        PageHelper.startPage(page,rows);
        List<T> list = mapper.select(t);
        return new PageInfo<>(list);
    }
}
